package com.pucheng.bracelet.ui.activity.watch

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.observe
import androidx.recyclerview.widget.LinearLayoutManager
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.ext.CLOCK_DATA_INDEX_KEY
import com.pucheng.bracelet.app.ext.CLOCK_DATA_KEY
import com.pucheng.bracelet.app.ext.WATCH_INFO
import com.pucheng.bracelet.app.util.click
import com.pucheng.bracelet.databinding.FragmentClockEditBinding
import com.pucheng.bracelet.databinding.FragmentWatchEditBinding
import com.pucheng.bracelet.ui.adapter.*
import com.pucheng.bracelet.viewmodel.bracelet.WatchViewModel
import com.pucheng.bracelet.viewmodel.clock.ClockViewModel


/**
 * A simple [Fragment] subclass.
 * Use the [WatchEditFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class WatchEditFragment(private val callback: () -> Unit) : Fragment() {
    private lateinit var mViewModel: WatchViewModel
    private lateinit var mViewBinding: FragmentWatchEditBinding
    private var watchData: WatchData? = null
    private val mAdapter = ColorAdapter { index: Int, _: ColorData ->
//        mViewModel.selectColorData(index)
    }
    private val mTimeAdapter = TextAdapter { index: Int, textData: TextData ->
        mViewBinding.tvTime.text = textData.text
//        mViewModel.selectTimeData(index)
    }
    private val mTimeUpAdapter = TextAdapter { index: Int, textData: TextData ->
        mViewBinding.tvTimeUp.text = textData.text
//        mViewModel.selectTimeUpData(index)
    }
    private val mTimeDownAdapter = TextAdapter { index: Int, textData: TextData ->
        mViewBinding.tvTimeDown.text = textData.text
//        mViewModel.selectTimeDownData(index)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            watchData = it.getSerializable(WATCH_INFO) as WatchData

        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mViewModel = ViewModelProvider(requireActivity()).get(WatchViewModel::class.java)
        mViewBinding = FragmentWatchEditBinding.inflate(
            LayoutInflater.from(requireContext()),
            container,
            false
        )
        mViewBinding.apply {
//            watchData?.apply {
//                mViewModel.editWatchData.value = this
//            }
            viewModel=mViewModel
            lifecycleOwner = viewLifecycleOwner
            rvTextColor.apply {
                adapter = mAdapter
                layoutManager = LinearLayoutManager(
                    requireContext(),
                    LinearLayoutManager.HORIZONTAL,
                    false
                )
            }
            rvTime.apply {
                adapter = mTimeAdapter
                layoutManager = LinearLayoutManager(
                    requireContext(),
                    LinearLayoutManager.HORIZONTAL,
                    false
                )
            }
            rvTimeUp.apply {
                adapter = mTimeUpAdapter
                layoutManager = LinearLayoutManager(
                    requireContext(),
                    LinearLayoutManager.HORIZONTAL,
                    false
                )
            }
            rvTimeDown.apply {
                adapter = mTimeDownAdapter
                layoutManager = LinearLayoutManager(
                    requireContext(),
                    LinearLayoutManager.HORIZONTAL,
                    false
                )
            }
            tvCancel.click {
                callback?.invoke()
            }
            tvSave.click {
//                mViewModel.saveWatch()
                callback?.invoke()
            }
            tvSelectPic.click {

            }
            tvSelectDefault.click {

            }
            tvTime.click {
                tvTimeArrow.setImageDrawable(resources.getDrawable(R.mipmap.down_step_gray))
                rvTime.apply {
                    visibility=if (isVisible)  View.GONE else View.VISIBLE
                }
            }
            tvTimeUp.click {
                tvTimeUpArrow.setImageDrawable(resources.getDrawable(R.mipmap.down_step_gray))
                rvTimeUp.apply {
                    visibility=if (isVisible)  View.GONE else View.VISIBLE
                }
            }
            tvTimeDown.click {
                tvTimeDownArrow.setImageDrawable(resources.getDrawable(R.mipmap.down_step_gray))
                rvTimeDown.apply {
                    visibility=if (isVisible)  View.GONE else View.VISIBLE
                }
            }
        }
//        mViewModel.apply {
//            colorData.observe(viewLifecycleOwner) {
//                mAdapter.submitList(it)
//            }
//            timeData.observe(viewLifecycleOwner) {
//                mTimeAdapter.submitList(it)
//            }
//            timeUpData.observe(viewLifecycleOwner) {
//                mTimeUpAdapter.submitList(it)
//            }
//            timeDownData.observe(viewLifecycleOwner) {
//                mTimeDownAdapter.submitList(it)
//            }
//        }
        return mViewBinding.root
    }


    companion object {
        @JvmStatic
        fun newInstance( watchData: WatchData, callback: () -> Unit):WatchEditFragment {
            return WatchEditFragment(callback).apply {
                arguments = Bundle().apply {
                    putSerializable(WATCH_INFO, watchData)
                }
            }
        }
    }


}